package com.wangnian.wangcai.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 创建日期 2016/12/9
 * 时间工具
 *
 * @author mac
 */
public class DateUtil {

    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
    /**
     * 格式 2015-08-07
     */
    public static final String DATEFORMAT_YYYYMMDD = "yyyy-MM-dd";

    /**
     * 格式 2015-08-07
     */
    public static final String YYYYMMDD = "yyyyMMdd";
    /**
     * 格式 15:25:30
     */
    public static final String DATEFORMAT_HHMMSS = "HH:mm:ss";

    /**
     * 格式 2015-08
     */
    public static final String DATEFORMAT_YYYYMM = "yyyy-MM";

    /**
     * 格式 2015-08-07 11:44:18
     */
    public static final String DATEFORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 格式 2015-08-07 11:44:18:591
     */
    public static final String DATEFORMAT_YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 格式 150807114418
     */
    public static final String DATEFORMAT_YYMMDDHHMMSS = "yyMMddHHmmss";

    /**
     * 格式 20140416142030
     */
    public static final String DATEFORMAT_TIMESTEMP_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    /**
     * 格式 20150807114418591
     */
    public static final String DATEFORMAT_TIMESTEMP = "yyyyMMddHHmmssSSS";

    /**
     * 格式 2015-08-13T16:49:02.039+0800
     */
    public static final String DATEFORMAT_YYYYMMDDTHHMMSSSSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    /**
     * 格式 2015-11-09T00:00:08.000+08:00
     */
    public static final String DateFormat_yyyyMMddTHHmmssSSSXXX = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    /**
     * 格式 2015-08-07 11:44
     */
    public static final String DATEFORMAT_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

    /**
     * 格式 11月22日
     */
    public static final String DATEFORMAT_MMDD = "yyyy年MM月dd日";

    /**
     * 日期转化为字符串
     *
     * @param format
     * @return
     */
    public static String parseToString(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            String strDate = df.format(date);
            return strDate;
        } catch (Exception ex) {
            logger.warn("日期转化为字符串出错,Date:{},format:{}", date, format);
            return null;
        }
    }


    /**
     * 字符串转化为日期
     *
     * @param str
     * @param format
     * @return
     */
    public static Date parseToDate(String str, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date date;
        try {
            date = df.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("11");
        }
        return date;
    }


    /**
     * 格式化日期
     */
    public static Date getFormatDate(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String strDate = sdf.format(date);
            return sdf.parse(strDate);
        } catch (Exception e) {
            logger.warn("格式化日期出错,Date:{},format:{}", date, format, e);
            return null;
        }
    }

    /**
     * 日期加天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, int days) {
        return localDate2Date(date2LocalDate(date).plusDays(days));
    }


    /**
     * 日期减天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date subDay(Date date, int days) {
        return localDate2Date(date2LocalDate(date).minusDays(days));
    }

    /**
     * 日期减天数
     *
     * @param days
     * @return
     */
    public static Date subDay(int days) {
        return localDate2Date(LocalDate.now().minusDays(days));
    }

    /**
     * 日期减天数
     *
     * @param days
     * @return
     */
    public static String subDay(int days, String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return LocalDate.now().minusDays(days).format(dateTimeFormatter);
    }

    public static Date getToday() {
        return getFormatDate(new Date(), DATEFORMAT_YYYYMMDD);
    }

    public static String getStringToday() {
        return parseToString(new Date(), DATEFORMAT_YYYYMMDD);
    }

    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * Date转LocalDate
     *
     * @param date
     */
    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static class NowDate {
        int hours;
        int minutes;

        public NowDate(int hours, int minutes) {
            this.hours = hours;
            this.minutes = minutes;
        }

        public int getHours() {
            return hours;
        }

        public void setHours(int hours) {
            this.hours = hours;
        }

        public int getMinutes() {
            return minutes;
        }

        public void setMinutes(int minutes) {
            this.minutes = minutes;
        }
    }

    public static NowDate getNowDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        return new NowDate(hours, minutes);
    }
}
